class Solution {
public:
    bool IsBalanced_Solution(TreeNode* pRoot) {
    	if(pRoot == nullptr)
    		return true;
    	int leftDp = 0,rightDp = 0;
		leftDp = TreeBalanced(pRoot->left);
		rightDp = TreeBalanced(pRoot->right);
    	if(leftDp - rightDp > 1 || leftDp - rightDp < -1 || !balanced)
    		return false;
    	return true;
    }

    int TreeBalanced(TreeNode* pRoot)
    {
    	if(pRoot == nullptr)
    		return 0;
    	if(!pRoot->left && !pRoot->right)
    		return 1;
    	int leftDp = 0 ,rightDp = 0;
		leftDp  = TreeBalanced(pRoot->left);
		rightDp = TreeBalanced(pRoot->right);
    	if(leftDp - rightDp > 1 || leftDp - rightDp < -1)
    		balanced = false;
    	return leftDp > rightDp ? leftDp + 1 : rightDp + 1;
    }
private:
	bool balanced = true;
};